1
|
|
|
/* eslint-env node */ |
2
|
|
|
/*jslint node: true */ |
3
|
|
|
'use strict'; |
4
|
|
|
|
5
|
|
|
let jsonfile = require('jsonfile'); |
6
|
|
|
|
7
|
|
|
let resources = jsonfile.readFileSync('build/data/resources.json'); |
8
|
|
|
let elements = jsonfile.readFileSync('build/data/elements.json'); |
9
|
|
|
|
10
|
|
|
// Generates an ion resource |
11
|
|
|
function generateResource(element, charge){ |
12
|
|
|
let name = generateName(element, charge); |
13
|
|
|
addResource(name, charge, element); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
// Adds an ion resource to the resources and elements list |
17
|
|
|
function addResource(name, charge, element){ |
18
|
|
|
if(!resources[name]){ |
19
|
|
|
resources[name] = {}; |
20
|
|
|
resources[name].elements = {}; |
21
|
|
|
resources[name].elements[element] = 1; |
22
|
|
|
let isotopeEnergy = resources[elements[element].main].energy; |
23
|
|
|
resources[name].energy = isotopeEnergy - resources['e-'].energy * charge; |
24
|
|
|
resources[name].html = element + htmlPostfix(charge); |
25
|
|
|
resources[name].charge = charge; |
26
|
|
|
resources[name].type = ['ion']; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if(elements[element].includes.indexOf(name) === -1){ |
30
|
|
|
elements[element].includes.push(name); |
31
|
|
|
} |
32
|
|
|
if(charge < 0 && elements[element].anions.indexOf(name) === -1){ |
33
|
|
|
elements[element].anions.push(name); |
34
|
|
|
} |
35
|
|
|
if(charge > 0 && elements[element].cations.indexOf(name) === -1){ |
36
|
|
|
elements[element].cations.push(name); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Generates the name of a ion, e.g. O3+ |
41
|
|
|
function generateName(element, i) { |
42
|
|
|
if (i === 0) { |
43
|
|
|
return element; |
44
|
|
|
} |
45
|
|
|
let postfix = ''; |
46
|
|
|
if(Math.abs(i) > 1){ |
47
|
|
|
postfix = Math.abs(i); |
48
|
|
|
} |
49
|
|
|
postfix += getSign(i); |
50
|
|
|
return element + postfix; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function getSign(number){ |
54
|
|
|
return number > 0 ? '+': '-'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Generates the HTML postfix of an ion (+ or -) |
58
|
|
|
function htmlPostfix(index) { |
59
|
|
|
let postfix = ''; |
60
|
|
|
if(index === 0){ |
61
|
|
|
return postfix; |
62
|
|
|
} |
63
|
|
|
postfix = Math.abs(index).toString(); |
64
|
|
|
postfix = postfix === '1' ? '' : postfix; |
65
|
|
|
return '<sup>' + postfix + getSign(index) + '</sup>'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
let minElectronegativity = Number.MAX_VALUE; |
69
|
|
|
let maxElectronegativity = -Number.MAX_VALUE; |
70
|
|
|
let redox = {}; |
71
|
|
|
for (let element in elements) { |
72
|
|
|
elements[element].includes = elements[element].includes || []; |
73
|
|
|
elements[element].anions = elements[element].anions || []; |
74
|
|
|
elements[element].cations = elements[element].cations || []; |
75
|
|
|
|
76
|
|
|
if(elements[element].electronegativity > 0 && elements[element].electronegativity < minElectronegativity){ |
77
|
|
|
minElectronegativity = elements[element].electronegativity; |
78
|
|
|
} |
79
|
|
|
if(elements[element].electronegativity > maxElectronegativity){ |
80
|
|
|
maxElectronegativity = elements[element].electronegativity; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
let energies = {}; |
84
|
|
|
let charge = -1; |
85
|
|
|
for (let energy of elements[element].electron_affinity || []) { |
86
|
|
|
generateResource(element, charge); |
87
|
|
|
energies[charge] = -energy; |
88
|
|
|
charge--; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
energies[0] = 0; |
92
|
|
|
|
93
|
|
|
charge = 1; |
94
|
|
|
for (let energy of elements[element].ionization_energy || []) { |
95
|
|
|
generateResource(element, charge); |
96
|
|
|
energies[charge] = energy; |
97
|
|
|
charge++; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
let cummulative = {}; |
101
|
|
|
for(let charge in energies){ |
102
|
|
|
cummulative[charge] = cumulativeEnergy(energies, charge); |
103
|
|
|
} |
104
|
|
|
if(typeof redox[element] === 'undefined'){ |
105
|
|
|
redox[element] = cummulative; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* Calculates the cummulative energy of a redox level. |
110
|
|
|
The logic is the following: the redox array gives how much energy it costs |
111
|
|
|
to go from a level to the next, e.g. from +2 to +3. This function calculates |
112
|
|
|
how much it takes to go from level 0 to x by summing each successive level */ |
113
|
|
|
function cumulativeEnergy(redox, level) { |
114
|
|
|
let energy = 0; |
115
|
|
|
let start = Math.min(0, level); |
116
|
|
|
let end = Math.max(0, level); |
117
|
|
|
for (let i = start; i <= end; i++) { |
118
|
|
|
energy += redox[i]; |
119
|
|
|
} |
120
|
|
|
if (level < 0) { |
121
|
|
|
energy = -energy; |
122
|
|
|
} |
123
|
|
|
return energy; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
for (let element in elements) { |
127
|
|
|
let power = elements[element].electronegativity-minElectronegativity; |
128
|
|
|
elements[element].negative_factor = Math.pow(10, power); |
129
|
|
|
power = maxElectronegativity-elements[element].electronegativity; |
130
|
|
|
elements[element].positive_factor = Math.pow(10, power); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
jsonfile.writeFileSync('build/data/resources.json', resources, { |
134
|
|
|
spaces: 2 |
135
|
|
|
}); |
136
|
|
|
jsonfile.writeFileSync('build/data/elements.json', elements, { |
137
|
|
|
spaces: 2 |
138
|
|
|
}); |
139
|
|
|
jsonfile.writeFileSync('build/data/redox.json', redox, { |
140
|
|
|
spaces: 2 |
141
|
|
|
}); |
142
|
|
|
|